<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="门禁卡状态:" prop="accessstatus">
          <el-switch v-model="formData.accessstatus" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item label="部门:" prop="department">
          <el-input v-model="formData.department" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="考期状态:" prop="examstatus">
          <el-switch v-model="formData.examstatus" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item
            label="头像"
            label-width="80px"
        >
          <div
              style="display:inline-block"
              @click="openHeaderChange"
          >
            <img
                v-if="formData.faceimg"
                alt="头像"
                class="header-img-box"
                :src="(formData.faceimg && formData.faceimg.slice(0, 4) !== 'http')?path+formData.faceimg:formData.faceimg"
            >
            <div
                v-else
                class="header-img-box"
            >从媒体库选择</div>
          </div>
        </el-form-item>

        <el-form-item label="身份证地址:" prop="idaddr">
          <el-input v-model="formData.idaddr" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="身份证号:" prop="idcard">
          <el-input v-model="formData.idcard" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="身份证照片:" prop="idcardimg">
          <el-input v-model="formData.idcardimg" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="进出状态:" prop="ishere">
          <el-switch v-model="formData.ishere" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item label="职位:" prop="job">
          <el-input v-model="formData.job" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="员工民族:" prop="nation">
          <el-input v-model="formData.nation" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="员工籍贯:" prop="native">
          <el-input v-model="formData.native" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="进出状态:" prop="outinstatus">
          <el-switch v-model="formData.outinstatus" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item label="员工性别:" prop="sex">
          <el-switch v-model="formData.sex" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item label="员工手机号:" prop="tel">
          <el-input v-model="formData.tel" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="员工姓名:" prop="username">
          <el-input v-model="formData.username" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createStaff,
  updateStaff,
  findStaff
} from '@/api/staff'

defineOptions({
    name: 'StaffForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'

const route = useRoute()
const router = useRouter()

const type = ref('')
const formData = ref({
            accessstatus: false,
            department: '',
            examstatus: false,
            faceimg: '',
            idaddr: '',
            idcard: '',
            idcardimg: '',
            ishere: false,
            job: '',
            nation: '',
            native: '',
            outinstatus: false,
            sex: false,
            tel: '',
            username: '',
        })
// 验证规则
const rule = reactive({
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findStaff({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data.restaff
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
}

init()
// 保存按钮
const save = async() => {
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return
            let res
           switch (type.value) {
             case 'create':
               res = await createStaff(formData.value)
               break
             case 'update':
               res = await updateStaff(formData.value)
               break
             default:
               res = await createStaff(formData.value)
               break
           }
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
